/**
 * 
 */
package org.jftstool.indexer.document.formater;

import java.io.FileOutputStream;
import java.util.Map;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jftstool.fileParserFramework.metadata.DocumentMetaData;
import org.jftstool.indexer.document.DBDocument;
import org.jftstool.indexer.document.DBField;
import org.jftstool.indexer.document.exceptions.DocumentFormaterException;
import org.xml.sax.ContentHandler;

/**
 * @author grupo JFTS
 *
 */
public class DefaultDocumentFormater implements IDocumentFormater {
	private Document xmlDocument = null;
	
	public DefaultDocumentFormater(){
		xmlDocument = new Document();
	}
	
	@Override
	public Document formatDocument(ContentHandler resultHandler, DocumentMetaData metaData) throws DocumentFormaterException {
		try{
			xmlDocument.detachRootElement();
			String text = resultHandler.toString();
			Element root = new Element("doc");
			
			//Id field
			Element fieldId = new Element("field");
			fieldId.setAttribute("name", "id");
			fieldId.setText(metaData.getProperty(DocumentMetaData.URI));
			
			//Content Field
			Element fieldContent = new Element("field");
			fieldContent.setAttribute("name", "content");
			fieldContent.setText(text);
			
			//Category Field
			Element fieldCategory = new Element("field");
			fieldCategory.setAttribute("name", "category");
			fieldCategory.setText(metaData.getProperty("ontClassPath"));		
		
			
			//Language Field
			Element fieldLanguage = new Element("field");
			fieldLanguage.setAttribute("name", "language");
			fieldLanguage.setText(metaData.getProperty(DocumentMetaData.CONTENT_LANGUAGE));		
			
			
			//Add fields
			root.addContent(fieldId);
			root.addContent(fieldContent);
			root.addContent(fieldCategory);
			root.addContent(fieldLanguage);
			
			xmlDocument.addContent(root);
			
		}
		catch(Throwable e){
			throw new DocumentFormaterException("Error formating document");
		}
		return xmlDocument;		
	}
	
	public void writeDocumentToFile(String path) throws DocumentFormaterException {
		XMLOutputter outputter = new XMLOutputter (Format.getPrettyFormat());
		try{
			outputter.output (xmlDocument,new FileOutputStream (path));
		   }
		catch (Exception e){
			throw new DocumentFormaterException("Error, can't write document to file: " + path);
		}
	}

	@Override
	public Document formatDocument(DBDocument dbDoc, ContentHandler resultHandler, DocumentMetaData metaData) throws DocumentFormaterException {
		try{		
			xmlDocument.detachRootElement();
			String text = resultHandler.toString();
			Map<String, DBField> fields = dbDoc.getFields();
			DBField dbField = null;
			Element e = null;
			
			Element root = new Element("doc");
			
			//Set document field
			Element fieldContent = new Element("field");
			fieldContent.setAttribute("name", dbDoc.getDocumentField());
			fieldContent.setText(text);
			root.addContent(fieldContent);
			
			//Set the rest of the db fields
			String[] keys =  fields.keySet().toArray(new String[fields.keySet().size()]);
			for(String key : keys){
				dbField = fields.get(key);
				e = new Element("field");
				e.setAttribute("name", dbField.getName());
				e.setText(_getStringFromField(dbField));
				root.addContent(e);				
			}
			
			//Language field
			Element fieldLanguage = new Element("field");
			fieldLanguage.setAttribute("name", "language");
			fieldLanguage.setText(metaData.getProperty(DocumentMetaData.CONTENT_LANGUAGE));		
			root.addContent(fieldLanguage);
			
			xmlDocument.addContent(root);
	
		}
		catch(Throwable e){
			throw new DocumentFormaterException("Error formating document");
		}
		return xmlDocument;		
	}

	
	private String _getStringFromField(DBField field){
		return  field.getValue().toString();
	}
}